using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PHSRAG.Insight21.CDS;
using PHSRAG.Insight21.Humans;
using PHSRAG.Insight21.Policy;
using System.IO;
using PHSRAG;

/// <summary>
/// Summary description for UIUtility
/// </summary>
public class UIUtility
{
	public UIUtility()
	{
		//
		// TODO: Add constructor logic here
		//
	}

	/// <summary>
	/// Writes the contents of the file to the response stream
	/// </summary>
	/// <param name="fname">Name of the file</param>
	/// <param name="forceDownload">Whether or not show the File Download dialogue</param>
	/// <param name="content">the contents of the file</param>
	public static void DownloadFile(string filename, bool forceDownload, byte[] content)
	{
		HttpContext.Current.Response.Clear();
		HttpContext.Current.Response.ClearHeaders();
		if (forceDownload)
		{
			HttpContext.Current.Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}", filename));
		}
		HttpContext.Current.Response.AddHeader("content-length", Convert.ToString(content.Length));
		HttpContext.Current.Response.BinaryWrite(content);
		HttpContext.Current.Response.Flush();
		HttpContext.Current.ApplicationInstance.CompleteRequest();
	}

	public static Document GetConsentFormWithFooter(ICachePolicy cachePolicy, ProtocolDocument protocolDoc)
	{
		InsightServiceUtility.DocumentUtility docUtil = new InsightServiceUtility.DocumentUtility();
		
		Document doc = new Document(cachePolicy);
		doc.Blob = docUtil.GetConsentFormWithFooter(protocolDoc.Id);
		doc.Name = "ConsentForm.pdf";
		return doc;
	}

    public static string GetFullDocStagingFolderPath()
    {
        string docStageFolder = PHSRAG.Utility.Settings.GetKeyValue("DocumentStagingFolder", "DocumentStagingFolder");
        return string.Format(@"\\{0}",
                Path.Combine(HttpContext.Current.Server.MachineName, docStageFolder));
    }

    public static void TransmitToClient(DataSet admPersonIdsForXML, string supervisorName, bool filterByLatestReportingPeriod)
    {

        string docStageFolderEntry = PHSRAG.Utility.Settings.GetKeyValue("DocumentStagingFolder", "DocumentStagingFolder");
        string docStagingFolder = string.Format(@"\\{0}",
                         Path.Combine(HttpContext.Current.Server.MachineName, docStageFolderEntry));

        InsightServiceUtility.DocumentUtility documentUtility = new InsightServiceUtility.DocumentUtility();
        documentUtility.Timeout = -1;
        string outputFile = documentUtility.GetDetailedReport(admPersonIdsForXML, supervisorName, docStagingFolder, filterByLatestReportingPeriod);

        if (outputFile == null)
        {
            throw new Exception("View Application (PDF): GetApplicationDocument returned a NULL value. Output file is empty");
        }
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        //HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=Application.pdf");
        HttpContext.Current.Response.ContentType = "Application/pdf";
        try
        {
            //example: transmitFilePath = e:\inetpub\wwwroot\stage-insight.partners.org\DocumentStagingFolder
            string transmitFilePath = Path.Combine(
                HttpContext.Current.Server.MapPath("~//" + docStageFolderEntry), Path.GetFileName(outputFile));
            HttpContext.Current.Response.TransmitFile(outputFile);
            HttpContext.Current.Response.End();
        }
        catch (Exception ex)
        {
            PHSRAG.Utility.ExceptionReport.WriteToEventLog(String.Format("Failed while transmitting the file {0} to the client.", outputFile),
           ex, true, PHSRAG.Utility.ExceptionReport.ReportType.Error);
        }
    }


    public static string InfoEdLandingPageURLKey = "InfoEdLandingPageURL";
    public static string IsInfoEdReadyForSSO = "IsInfoEdReadyForSSO";
    public static string InfoEdTargetPageForAgreementsProjectDetails = "InfoEdTargetPageForAgreementsProjectDetails";
    public static string InfoEdTargetPageForAgreementContractDetails = "InfoEdTargetPageForAgreementContractDetails";
    public static string InfoEdTargetPageForProtocolDetails = "InfoEdTargetPageForProtocolDetails";



    //public static string GetInfoEdLandingPageURL()
    //{

    //}
}
